//
//  YGProductCategoryView.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/11.
//

// 产品中心头部- 分类 -
#import "YGProductCategoryView.h"
#import "YGSelectItemView.h"
#import "YGSelectAniBaseView.h"
#import "YGGoodsSelectModel.h"
#import "YGProductBLL.h"
@interface YGProductCategoryView ()<YGSelectItemViewDelegate>

@property (nonatomic, strong) YGSelectItemView *firstItemView;
@property (nonatomic, strong) YGSelectItemView *secondItemView;
@property (nonatomic, strong) YGSelectAniBaseView *baseView;
@property (nonatomic, strong) NSArray  *secondViewArray;
@property (nonatomic, strong) NSArray  *firstViewArray;

@end
@implementation YGProductCategoryView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.baseView.height = self.firstItemView.height;
    self.baseView.width = self.width;
    self.secondItemView.left = self.firstItemView.right;
    self.secondItemView.height = self.firstItemView.height; //同等高度
}

- (void)initView{
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = UIColorFromRGBAlpha(0x333333, 0.5);
    bgView.userInteractionEnabled = YES;
    [self addSubview:bgView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBGViewAction)];
    [bgView addGestureRecognizer:tap];
    [self addSubview:bgView];
    
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.firstItemView];
    [self.baseView addSubview:self.secondItemView];

}

- (void)clickBGViewAction{
    [self hidden];
}


- (void)configureData:(NSArray *)datas{
    [self.firstItemView cnofigureData:datas];
    self.firstViewArray = datas;
    //默认选择第一个
    [self selectFirtItemViewWithModel:0];
    
}

- (void)selectFirtItemViewWithModel:(NSInteger)index{
    WeakSelf(self);
    self.secondViewArray = nil;
    YGGoodsSelectModel *model = [self.firstViewArray objectAtIndex:index];
    [YGProductBLL requestGoodsTypeLowerWithCategoryId:model.id Success:^(NSArray<YGGoodsSelectModel *> * _Nonnull model) {
        weakself.secondViewArray = model;
        [weakself.secondItemView cnofigureData:model];
        [weakself layoutSubviews];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)show{
    self.hidden = NO;
    [self layoutSubviews];
    [self.baseView show];
}

- (void)hidden{
    WeakSelf(self);
    [self.baseView hidden];
    [self.baseView setHiddenSuBlock:^{
        weakself.hidden = YES;
    }];
}

- (void)selectViewWithIndex:(NSInteger)index selectView:(YGSelectItemView *)selectView{
    if (selectView == self.firstItemView) {
        
        [self selectFirtItemViewWithModel:index];
    }else{
        if ([self.delegate respondsToSelector:@selector(selectViewWithIndex:selectView:)]) {
            [self.delegate selectViewWithIndex:index selectView:self];
        }
        [self hidden];
    }
    
}

- (YGSelectItemView *)firstItemView{
    if (!_firstItemView) {
        _firstItemView = [[YGSelectItemView alloc] initWithFrame:CGRectMake(0, 0, self.width/3.0, 0)];
        _firstItemView.backgroundColor = UIColorFromRGB(0xF5F5F5);
        _firstItemView.delegate = self;
    }
    return _firstItemView;
}

- (YGSelectItemView *)secondItemView{
    if (!_secondItemView) {
        _secondItemView = [[YGSelectItemView alloc] initWithFrame:CGRectMake(0, 0, self.width*2 / 3.0, 0)];
        _secondItemView.delegate = self;
    }
    return _secondItemView;
}

- (YGSelectAniBaseView *)baseView{
    if (!_baseView) {
        _baseView = [[YGSelectAniBaseView alloc] init];
    }
    return _baseView;
}

- (NSArray *)getSecondArray{
    return _secondViewArray;
}
@end
